<?php
class ControllerCommonHome extends Controller {

  public function index()
  {      
		$this->language->load('common/home');
		$this->document->title = $this->config->get('config_title');
		$this->document->description = $this->config->get('config_meta_description');
		$this->data['heading_title'] = sprintf($this->language->get('heading_title'), $this->config->get('config_name'));

        /*
		$this->load->model('setting/store');
		if (!$this->config->get('config_store_id')) {
			$this->data['welcome'] = html_entity_decode($this->config->get('config_description_' . $this->config->get('config_language_id')), ENT_QUOTES, 'UTF-8');
		}
        else {
			$store_info = $this->model_setting_store->getStore($this->config->get('config_store_id'));

			if ($store_info) {
				$this->data['welcome'] = html_entity_decode($store_info['description'], ENT_QUOTES, 'UTF-8');
			}
            else {
				$this->data['welcome'] = '';
			}
		}
        */

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/home.tpl')) 
    {
			$this->template = $this->config->get('config_template') . '/template/common/home.tpl';
		} 
    else 
    {
			$this->template = 'default/template/common/home.tpl';
		}

		$this->children = array(
//			'common/column_right',
//			'common/column_left',
			'common/footer',
			'common/header'
		);
		
		/*
		$this->load->model('checkout/extension');
		$module_data = $this->model_checkout_extension->getExtensionsByPosition('module', 'home');
		$this->data['modules'] = $module_data;

		foreach ($module_data as $result) {
			$this->children[] = 'module/' . $result['code'];
		}
		*/

        // get images used for gallery
    $this->load->model('catalog/gallery');
    $this->load->model('tool/image');
    $this->data['imgs_intro'] = $this->model_catalog_gallery->getImageForGallery();

    for ($i = 0; $i < count($this->data['imgs_intro']); $i++) 
    {
      $kp = $this->data['imgs_intro'][$i];
      if (file_exists(DIR_IMAGE . $kp['image'])) 
      {
        $this->data['imgs_intro'][$i]['original'] = $this->model_tool_image->resize($kp['image'], 630, 240);                
        $this->data['imgs_intro'][$i]['thumb'] = $this->model_tool_image->resize($kp['image'], 64, 64);
      } 
      else 
      {
        $this->data['imgs_intro'][$i]['original'] = $this->model_tool_image->resize('no_image.jpg', 630, 240);
        $this->data['imgs_intro'][$i]['thumb'] = $this->model_tool_image->resize('no_image.jpg', 64, 64);
      }
    }

    // get top news
    $this->load->model('cf/news');
    $results = $this->model_cf_news->getItems(array('type' => 'TOP', 'limit' => 2));
    foreach ($results as $result) 
    {
      $this->data['top_news'][] = array(
        'news_id' => $result['news_id'],
        'title' => $result['title'],
        'description' => html_entity_decode($result['description']),
        'thumb' => $this->model_tool_image->resize($result['image'], 70, 70)
      );
    }

    // get bottom news
    $this->load->model('cf/news');
    $this->load->helper('text');
    $this->data['bottom_news'] = $this->model_cf_news->getItems(array('type' => 'BOTTOM', 'limit' => 2));

    // get new products
    $this->load->model('catalog/product');
    $this->load->model('tool/seo_url'); 
    $this->data['new_products'] = $this->model_catalog_product->getProductsOnCondition(array('new' => true, 'limit' => 3));    
    for ($i = 0; $i < count($this->data['new_products']); $i++) 
    {
      $this->data['new_products'][$i]['pic'] = $this->model_tool_image->resize($this->data['new_products'][$i]['image'], 312, 190);
      $this->data['new_products'][$i]['description'] = word_limiter(strip_tags(html_entity_decode($this->data['new_products'][$i]['description'])), 10);
      $this->data['new_products'][$i]['href'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/product&product_id=' . $this->data['new_products'][$i]['product_id']);
    }
    
    // add scripts & styles
    $this->document->addScript('catalog/view/javascript/jquery/jquery.galleryview-1.1.js');
    $this->document->addScript('catalog/view/javascript/jquery/jquery.easing.1.3.js');
    $this->document->addScript('catalog/view/javascript/jquery/jquery.timers-1.1.2.js'); 
    $this->document->addScript('catalog/view/javascript/jquery/transify.js');
    $this->document->addScript('catalog/view/javascript/jquery/jquery.cf.js');
    $this->document->addStyle('catalog/view/theme/' . $this->config->get('config_template') . '/stylesheet/home.css');

		$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
	}

}
